import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'
const baseUrlScheme = CommonConfig.schemaUrl + '/v1'
const baseUrlMachineStatus = CommonConfig.machinestatusUrl + '/v1'

// const baseUrlTpm = CommonConfig.url3

import qs from 'qs'

const qsConfig = {
  arrayFormat: 'repeat',
  skipNulls: true,
  format: 'RFC3986'
}

// 获取生产组织架构-树形选择器结构
export function getOrganize () {
  const newUrl = baseUrlScheme + '/scheme_tree'

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

// 通过所在位置信息获取工位信息
export function getStation (level, schemeId) {
  const newUrl = baseUrlScheme + '/workcenter_no_page'

  let reqData = {
    params: {
      level,
      schemeId
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取机台状态
export function getMachineStatus (keyword, pageNum, pageSize, searchBy, status, workCenterId, positionId, level) {
  const newUrl = baseUrlMachineStatus + '/machine_status'

  const reqData = {
    params: {
      keyword: keyword || undefined,
      pageNum: pageNum,
      pageSize: pageSize,
      searchBy: searchBy || undefined,
      statusList: [status],
      workCenterIdList: [workCenterId],
      positionId: positionId || undefined,
      level: level || undefined
    },
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取机台详情(开动率、报警记录、停机时长)
export function getMachineStatusDetails (boxId, deviceId) {
  const newUrl = baseUrlMachineStatus + '/machine_status_details'

  const reqData = {
    params: {
      boxId: boxId,
      deviceId: deviceId
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取停机记录
export function getDowntimeDetails (boxId) {
  const newUrl = baseUrlMachineStatus + '/stop_cause_details'

  const reqData = {
    params: {
      boxId: boxId
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取单词循环零件加工数量
export function getProcessCount (boxId, deviceId) {
  const newUrl = `${CommonConfig.machinestatusUrl}/v1/machine_status/process_count`

  const reqData = {
    params: {
      boxId: boxId,
      deviceId: deviceId
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取应完成数量
export function getMachineExpectedCount (boxId, deviceId) {
  const newUrl = baseUrlMachineStatus + '/machine_expected_count'

  const reqData = {
    params: {
      boxId: boxId,
      deviceId: deviceId
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取用时和开动率
export function getMachineTimeRate (boxId, deviceId) {
  const newUrl = baseUrlMachineStatus + '/machine_time_rate'

  const reqData = {
    params: {
      boxId: boxId,
      deviceId: deviceId
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取开动率图表数据
export function getMachineRateChart (boxId, deviceId) {
  const newUrl = baseUrlMachineStatus + '/machine_rate_chart'

  const reqData = {
    params: {
      boxId: boxId,
      deviceId: deviceId
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取正常循环列表
export function getNormalWorkpieces (id, since, before) {
  const newUrl = `${baseUrlMachineStatus}/boxes/${id}/normal_workpieces`

  const reqData = {
    params: {
      start: since,
      end: before
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 根据盒子ID 列举状态持续时间明细
export function getDowntimeDuration (id, since, before, code) {
  const newUrl = `${baseUrlMachineStatus}/boxes/${id}/device_status_durations`

  const reqData = {
    params: {
      start: since,
      end: before,
      code
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}
// 编辑停机明细
export function editDowntimeDuration (id, params) {
  const newUrl = `${baseUrlMachineStatus}/boxes/${id}/device_status_durations`

  return axiosInstance.put(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}

// 根据盒子ID 列举报警明细
export function getDeviceAlarms (id, since, before) {
  const newUrl = `${baseUrlMachineStatus}/boxes/${id}/device_alarms`

  const reqData = {
    params: {
      start: since,
      end: before
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}
// 根据盒子ID 列举异常报工明细
export function getAbnormalWorkpieces (id, since, before) {
  const newUrl = `${baseUrlMachineStatus}/boxes/${id}/abnormal_workpieces`

  const reqData = {
    params: {
      start: since,
      end: before
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}
// 异常循环标记正常
export function abnormalWorkpiecesRelease (timestamps) {
  const newUrl = `${baseUrlMachineStatus}/abnormal_workpieces:release`

  const params = {
    timestamps
  }
  return axiosInstance.put(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}

// 查询当天班制的开始时间
export function getStartDateTime (boxId, deviceId, date) {
  const url = `${baseUrlMachineStatus}/machines_status/shifts/start_date_time`

  const reqData = {
    boxId,
    deviceId,
    date
  }

  return axiosInstance.get(url, { params: reqData }).then((res) => {
    return Promise.resolve(res)
  })
}

export function getMachineTimelineData (boxId, deviceId, date) {
  const url = `${baseUrlMachineStatus}/machine_time_line`

  const reqData = {
    boxId,
    deviceId,
    date
  }

  return axiosInstance.get(url, { params: reqData })
}

// 获取产品循环统计

export function getProductList (id, since, before) {
  const newUrl = `${baseUrlMachineStatus}/boxes/${id}/normal_workpieces:circular`

  const reqData = {
    params: {
      start: since,
      end: before
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取产品循环统计

export function getDetailedCount (id, since, before) {
  const newUrl = `${baseUrlMachineStatus}/boxes/${id}/timeline/detailed:count`

  const reqData = {
    params: {
      start: since,
      end: before
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}
/**
 * 时间线报表
 * @param {*}
 */
export function getListTimeLine ({ date, pageNum, pageSize, deviceTreeReqVO, workshopId }) {
  const newUrl = `${baseUrlMachineStatus}/machine/list_time_line`
  const params = {
    date,
    pageNum,
    pageSize,
    deviceTreeReqVO,
    workshopId
  }
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}
